# -*- encoding:utf8 -*-
from attendance.datetimestub import datetime,datetime_orig

#sys.modules['django.utils.timezone']=sys.modules['datetime']
from django.test import TestCase
from datetime import date,datetime

#from attend import record
#import attend
#attend.datetime =datetime
from recordproc import record
from reportproc import report
from staff.models import StaffInfo
from attendance.models import AttendRecord,AttendReport,LeaveModel,LeaveApplication,AvaiLeave
from attendance.leave import submit_leave,approve_leave_app
from attendance.convert import ts2is,t2i
from timeproc import subtract,filter_leave,filter_noonrest,filter_continueday_overtime,get_shoulde_checktimes
from django.dispatch import receiver,Signal
#from signal_handel import wn_switch_approved
from attendance.leave.signal_handel import post_application_save,pre_application_save
from signal_handel import _application_related_day
from timeproc import get_shoulde_checktimes

class AttendTest(TestCase):
    def setUp(self):
        self.b,created=StaffInfo.objects.get_or_create(employee_id='AE1857',cn_name=u'白X',startjob_date='2014-01-04',on_board_date='2015-09-07')
        self.staff,createed=StaffInfo.objects.get_or_create(employee_id='AE1856',cn_name=u'何宇林',startjob_date='2014-01-04',on_board_date='2015-09-07',
                                                            workshift='8:30-17:30',func_leader=self.b)
        
        ## AvaiLeave.objects.get_or_create(staff=self.staff,category='annual_leave',span='20:00') 
    
    def test_record(self):
        record(self.staff, date(2016,2,9),timestr="8:30 17:50")
        row=AttendRecord.objects.get(staff=self.staff,date='2016-02-09')
        self.assertEqual(row.enter_time,'08:30')
        self.assertEqual(row.leave_time,'17:50')
        
        record(self.staff, date(2016,2,10),timestr="8:29")
        row=AttendRecord.objects.get(staff=self.staff,date='2016-02-10')
        self.assertEqual(row.enter_time,'08:29')
        self.assertEqual(row.leave_time,'08:29')     
        self.assertEqual(row.absent,'04:00')
    
    def test_report(self):
        record(self.staff, date(2016,2,5),timestr="8:50 17:50")
        record(self.staff, date(2016,2,9),timestr="8:30 17:50")
        report(self.staff,date(2016,2,9))
        row=AttendReport.objects.get(staff=self.staff,start_date='2016-02-01')
        self.assertEqual(row.late2,'1')
    
    def test_afterleave(self):
        # 先有迟到
        self.test_report()
        # 提交一个请假，然后直接同意它
        leave_app = submit_leave(self.staff, category='sick_leave', start_time='2016-02-05 08:30', end_time='2016-02-05 12:30', reason='ttt')
        approve_leave_app(leave_app)
        row=AttendReport.objects.get(staff=self.staff,start_date='2016-02-01')
        self.assertEqual(row.late2,'0') 
    
    def test_misscheck(self):
        # 先漏打卡
        record(self.staff, date(2016,2,5),timestr="8:20")
        report(self.staff,date(2016,2,9))
        row=AttendReport.objects.get(staff=self.staff,start_date='2016-02-01')
        self.assertEqual(row.absent,'0.50')
        
        # 提交一个miss_check申请
        leave_app = submit_leave(self.staff, category='miss_check', start_time='2016-02-05 08:30', end_time='2016-02-05 17:30', reason='ttt')
        approve_leave_app(leave_app)   
        row=AttendReport.objects.get(staff=self.staff,start_date='2016-02-01')
        self.assertEqual(row.absent,'0')
        
        
        # 先漏打卡
        record(self.staff, date(2016,2,9),timestr="8:20")
        report(self.staff,date(2016,2,9))
        row=AttendReport.objects.get(staff=self.staff,start_date='2016-02-01')
        self.assertEqual(row.absent,'0.50')
        
        # 提交一个miss_check申请
        leave_app = submit_leave(self.staff, category='miss_check', start_time='2016-02-09 08:30', end_time='2016-02-09 16:30', reason='ttt')
        approve_leave_app(leave_app)   
        row=AttendReport.objects.get(staff=self.staff,start_date='2016-02-01')
        self.assertEqual(row.absent,'0')  
        self.assertEqual(row.early_leave,'01:00')
    
    def test_get_swap_off(self):
        record(self.staff, date(2016,2,6),timestr="8:30 17:30")
        report(self.staff)
        self.assertEqual( AvaiLeave.objects.all().count(),1)
        self.assertEqual(AvaiLeave.objects.all()[0].span,'09:00')
        
    def test_wn_switch_approved(self):
        record(self.staff, date(2016,2,11),timestr='')
        rcd=AttendRecord.objects.get(staff=self.staff,date='2016-02-11')
        self.assertEqual(rcd.absent,'08:00')
        
        application=submit_leave(self.staff, 'sick_leave', '2016-02-10 08:30', '2016-02-18 17:30', 'dd')
        self.assertEqual(application.status,'pending')
        rcd=AttendRecord.objects.get(staff=self.staff,date='2016-02-11')
        self.assertEqual(rcd.absent,'08:00')        
        approve_leave_app(application)
        self.assertEqual(application.status,'approved')
        rcd=AttendRecord.objects.get(staff=self.staff,date='2016-02-11')
        self.assertEqual(rcd.absent,'00:00')        
    
    def test_get_shoulde_checktimes(self):
        sud_start,sud_end=get_shoulde_checktimes(self.staff, date(2016,2,16))
        self.assertEqual(sud_start,8*60+30)
        self.assertEqual(sud_end,17*60+30)
        
        leave=submit_leave(self.staff, 'sick_leave', '2016-02-16 08:30', '2016-02-16 17:30', 'dd')
        approve_leave_app(leave)
        
        sud_start,sud_end=get_shoulde_checktimes(self.staff, date(2016,2,16))
        self.assertEqual(sud_start,0)
        self.assertEqual(sud_end,0)  
    
    def test_application_related_day(self):
        application=submit_leave(self.staff, 'sick_leave', '2016-02-10 08:30', '2016-02-18 17:30', 'dd')   
        ls=_application_related_day(application)
        self.assertEqual(ls,[date(2016,2,day) for day in range(10,19)])
        
        
class RecordTest(TestCase):
    def setUp(self):
        self.b,created=StaffInfo.objects.get_or_create(employee_id='AE1857',cn_name=u'白X',startjob_date='2014-01-04',on_board_date='2015-09-07')
        self.staff,createed=StaffInfo.objects.get_or_create(employee_id='AE1856',cn_name=u'何宇林',startjob_date='2014-01-04',on_board_date='2015-09-07',
                                                                workshift='8:30-17:30',func_leader=self.b)  
        AttendRecord.objects.get_or_create(staff=self.staff,date='2016-02-16',enter_time='08:20',leave_time='22:30',overtime='3:30')
        
        #Signal.disconnect(post_application_save)
        #Signal.disconnect(pre_application_save)
        #Signal.disconnect(wn_switch_approved)
        
        #leaveapp,c=LeaveApplication.objects.get_or_create(staff=self.staff,status='approved',start_time='2016-02-18 08:30',end_time='2016-02-18 12:30')
        #LeaveModel.objects.get_or_create(staff=self.staff,start_time='2016-02-18 08:30',end_time='2016-02-18 12:30',
                                         #category='personal_leave',submit_leave=leaveapp)
        leave_app=submit_leave(self.staff,'personal_leave',start_time='2016-02-18 08:30',end_time='2016-02-18 12:30',reason='why')
        print('first is --',LeaveModel.objects.all().count())
        approve_leave_app(leave_app)
        #self.assertEqual(LeaveApplication.objects.all().count(),2)
        print(LeaveApplication.objects.all().count())
        print('secod is --',LeaveModel.objects.all().count())
        
    def test_subtract(self):
        print('--test subtract')
        self.assertEqual(subtract(ts2is("8:30-12:30"), ts2is("10:30-12:30")), [ts2is("8:30-10:30")])  # 后端临界
        self.assertEqual(subtract(ts2is("8:30-12:30"), ts2is("8:30-10:30")), [ts2is("10:30-12:30")])  # 前段临界
        self.assertEqual(subtract(ts2is("8:30-12:30"), ts2is("8:30-12:30")), [])  # 前后临界
        self.assertEqual(subtract(ts2is("8:30-12:30"), ts2is("9:00-11:00")),
                         [ts2is("8:30-9:00"), ts2is("11:00-12:30")])  # 中间段

        # 下面是不太可能出现的情况，
        self.assertEqual(subtract(ts2is("8:30-12:30"), ts2is("13:30-17:30")), [ts2is('8:30-12:30')])  # 不相交
        self.assertEqual(subtract(ts2is("13:30-17:30"), ts2is("8:30-12:30")), [ts2is('13:30-17:30')])

        self.assertEqual(subtract(ts2is("8:30-12:30"), ts2is("9:30-13:30")), [ts2is("8:30-9:30")])
        self.assertEqual(subtract(ts2is("13:30-17:30"), ts2is("16:00-17:30")), [ts2is("13:30-16:00")])

    def test_filter_leave(self):
        self.assertEqual(filter_leave([ts2is("8:30-12:30"), ts2is("13:30-17:30")], [ts2is("9:00-11:00")]),
                         [ts2is("8:30-9:00"), ts2is('11:00-12:30'), ts2is("13:30-17:30")])
        self.assertEqual(filter_leave([ts2is("8:30-12:30"), ts2is("13:30-17:30")], [ts2is("9:00-16:00")]),
                         [ts2is('8:30-9:00'), ts2is('16:00-17:30')])

    def test_fiter_noonrest(self):
        self.assertEqual(filter_noonrest([ts2is("8:30-17:30")], ts2is("12:30-13:30")),
                         [ts2is("8:30-12:30"), ts2is("13:30-17:30")])

    def test_filter_overtime(self):
        self.assertEqual(filter_continueday_overtime([ts2is("8:30-12:30"), ts2is("13:30-17:30")], t2i("1:30")),
                         [ts2is("10:00-12:30"), ts2is("13:30-17:30")])

    def test_get_worktimes(self):
        self.assertEqual(get_shoulde_checktimes(self.staff,date(2016,2,16)), (t2i('8:30'),t2i('17:30')) )
        self.assertEqual(get_shoulde_checktimes(self.staff,date(2016,2,17)), (t2i("13:30"),t2i("17:30")))

        self.assertEqual(get_shoulde_checktimes(self.staff,date(2016,2,18)),(t2i("13:30"),t2i("17:30")))

        #self.assertEqual(get_shoulde_checktimes(leave=["13:30-17:30", ], continueday_overtime="2:30"), [])



